package matt.design;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class TasksSQLiteHelper extends SQLiteOpenHelper {

	public static final String TABLE_TASKS = "tasks";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_USER = "user";
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_DESC = "description";
	public static final String COLUMN_DUE = "due_date";
	public static final String COLUMN_START = "start_date";
	public static final String COLUMN_PRIORITY = "priority";
	public static final String COLUMN_GROUP = "group_name";
	public static final String COLUMN_REC = "recurring";
	public static final String COLUMN_COMP = "completed";
	public static final String COLUMN_ADDRESS = "address";
	public static final String COLUMN_LATITUDE = "latitude";
	public static final String COLUMN_LONGITUDE = "longitude";
	
	private static final String DATABASE_NAME = "tasks.db";
	private static final int DATABASE_VERSION = 1;
	
	private static final String DATABASE_CREATE = "create table "
			+ TABLE_TASKS + "( " 
			+ COLUMN_ID + " integer primary key autoincrement, "
			+ COLUMN_USER + " text not null, "
			+ COLUMN_NAME + " text not null, "
			+ COLUMN_DESC + " text not null, "
			+ COLUMN_DUE + " text not null, "
			+ COLUMN_START + " text not null, "
			+ COLUMN_PRIORITY + " integer not null, "
			+ COLUMN_GROUP + " text not null, "
			+ COLUMN_REC + " text not null, "
			+ COLUMN_COMP + " bool not null,"
			+ COLUMN_ADDRESS + " text not null,"
			+ COLUMN_LATITUDE + " text not null,"
			+ COLUMN_LONGITUDE + " text not null);";
	
	/**
	 * Method that takes in context and then the database name and version in order to locate the current database
	 * @param context
	 */
	public TasksSQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * Method that actually creates the columns and labels of the columns in the database
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE);
	}

	/**
	 * Upgrades the database with new information
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(TasksSQLiteHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS" + TABLE_TASKS);
		onCreate(db);
	}
}
